package com.kedacom.ctsp.authz.security.captcha;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.Properties;

/**
 * Google KaptchaService 实现
 *
 * @author sunchenjie
 * @create 2017-12-28 14:34
 **/
public class KaptchaService implements CaptchaService {

    private CaptchaProperties captchaProperties;
    private Properties props = new Properties();
    private Producer kaptchaProducer = null;
    private String sessionKeyValue = null;
    private String sessionKeyDateValue = null;

    public KaptchaService(CaptchaProperties captchaProperties) {

        this.captchaProperties = captchaProperties;

        //设置宽和高。
        this.props.put(Constants.KAPTCHA_IMAGE_WIDTH, captchaProperties.getImageWidth());
        this.props.put(Constants.KAPTCHA_IMAGE_HEIGHT, captchaProperties.getImageHeight());
        //kaptcha.border：是否显示边框。
        this.props.put(Constants.KAPTCHA_BORDER, "no");
        //kaptcha.textproducer.font.color：字体颜色
        this.props.put(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black");
        //kaptcha.textproducer.char.space：字符间距
        this.props.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "5");
        //设置字体。
        this.props.put(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, "40");
        //this.props.put(Constants.KAPTCHA_NOISE_COLOR, "");

        // 更多的属性设置可以在com.google.code.kaptcha.Constants类中找到。
        Config kaptchaConfig = new Config(this.props);
        this.kaptchaProducer = kaptchaConfig.getProducerImpl();
        this.sessionKeyValue = kaptchaConfig.getSessionKey();
        this.sessionKeyDateValue = kaptchaConfig.getSessionDate();
    }


    public String getSessionKeyValue() {
        return this.sessionKeyDateValue;
    }


    @Override
    public String sessionKeyValue() {
        return this.sessionKeyValue;
    }

    @Override
    public String sessionKeyDateValue() {
        return this.sessionKeyDateValue;
    }

    @Override
    public String createText() {
        return this.kaptchaProducer.createText();
    }

    @Override
    public BufferedImage createImage(String captchaText) {
        return this.kaptchaProducer.createImage(captchaText);
    }

    @Override
    public Boolean checkCaptcha(HttpServletRequest request, String captcha) {
        // 如果还不需要显示验证码
        if (checkWrongTimes(request, captchaProperties.getShowTimes())) {
            return true;
        }

        String currCaptcha = (String) request.getSession().getAttribute(this.sessionKeyValue);

        // 立刻移除
        request.getSession().removeAttribute(this.sessionKeyValue);

        // 判断是否匹配
        if (StringUtils.isBlank(captcha)) {
            return false;
        }
        if (StringUtils.isBlank(currCaptcha)) {
            return false;
        }
        if (!captcha.toLowerCase().equals(currCaptcha.toLowerCase())) {
            return false;
        }
        return true;
    }

    @Override
    public void setSessionValue(HttpSession session, String capText, Date date) {
        if (session != null) {
            session.setAttribute(this.sessionKeyValue, capText);
            session.setAttribute(this.sessionKeyDateValue, date);
        }
    }

}